<HEAD> <TITLE> while loops </TITLE> </HEAD> <BODY BACKGROUND="http://www.imaginary.com/LPMud/Graphics/RecycledPaper.jpg" TEXT=#000000 ALINK=#FFFF00 VLINK=#550000 LINK=#000099> <center> <H1> while loops </H1> </center>
<p>
LPC's while loop is identical to that provided by C.  Syntax is as follows:
<p>
<pre>
while (expression)
    statement;
</pre>
<p>
where statement may be replaced by a block of statements surrounded by
curly brackets:
<p>
<pre>
while (expression) {
    statement1;
    statement2;
    ...
}
</pre>
<p>
The statements inside the body of the while loop will be executed repeatedly
as long as the test expression evaluates to non-zero, however, the the
test expression is only tested at the top of the loop.  If the test expression
is zero prior to the execution of the loop, the loop will not be executed,
and execution continues after the loop.  If you want the loop to execute
at least once, use a <A HREF=http://www.imaginary.com/~beek/mudos/lpc/do_while.html>  do while </A> loop.  A <A HREF=http://www.imaginary.com/~beek/mudos/lpc/break.html>  break </A>; staement in the body
of the loop will terminate the loop, skipping immediately to the first
statement after the loop.  A <A HREF=http://www.imaginary.com/~beek/mudos/lpc/continue.html>  continue </A>; expression will go back to the 
top of the loop, check the test expression, and restart from the top as if
the bottom of the loop had been reached, skipping the remainder of the
statements in the loop.
<p>
<pre>
int test(int limit) {
    int total = 0;
    int j = 0;
    while (j < limit) {
        if ((j % 2) != 0)
            continue;
        total += j;
        j++;
    }
    return total;
}
</pre>
<p>
The result of this code fragment will be to sum all of the even numbers from
0 to limit - 1.
<p>
See also the summary of <A HREF=http://www.imaginary.com/~beek/mudos/lpc/loops.html>  loops </A>.
<p>
<HR> <ADDRESS> <A HREF=http://wagner.princeton.edu/~tim>  Tim Hollebeek </A> <p> <A HREF="http://www.imaginary.com/~beek/">  Beek </A>@ZorkMUD, Lima Bean, IdeaExchange, TMI-2, and elsewhere </ADDRESS>
<p>
